import router from "@src/router";
const navItems = {
  home: {
    icon: "/wxapp/static/navbar/navbar-index-icon.png",
    path: "/pages/index/main",
  },
  // 'scan': {
  //   icon: '/wxapp/static/navbar/navbar-scan-icon.png',
  //   path: '/pages/mineScan/main'
  // },
  balance: {
    icon: "/wxapp/static/navbar/icon_vipcard.png",
    class: "back",
    path: "/pages/balance/main",
  },
  codeOrder: {
    icon: "/wxapp/static/shop-mall/common/nav_return.png",
    class: "back",
    path: "code-order/pages/index/main",
  },
};

Component({
  externalClasses: ["title-class"],
  options: {
    multipleSlots: true, // 在组件定义时的选项中启用多slot支持
  },
  properties: {
    icons: {
      type: String,
      value: "",
    },
    title: {
      type: String,
      value: "",
    },
    fixed: {
      type: Boolean,
      value: true,
    },
    transparent: {
      type: Boolean,
      value: false,
    },
    backgroundColor: {
      type: String,
      value: "",
    },
    titleColor: {
      type: String,
      value: "",
    },
    loading: {
      type: Boolean,
      value: false,
    },
    loadingTitle: {
      type: String,
      value: "",
    },

    height: {
      type: Number,
      value: 0,
    },
    align: {
      type: String,
      value: "center",
    },
    ifLinear: {
      type: Boolean,
      value: false,
    },
    //是否用白色返回按钮
    supportWhiteBlack: {
      type: Boolean,
      value: false,
    },
    showBack: {
      type: Boolean,
      value: true,
    },
  },
  data: {
    back: false,
    statusBarHeight: 80,
    navBarHeight: 44,
    iconList: [],
  },
  observers: {
    icons: function (value) {
      this.parseIcons(value);
    },
  },

  lifetimes: {
    attached: function () {
      this.initNav();
      this.parseIcons(this.data.icons);
    },
    detached: function () {},
  },
  pageLifetimes: {
    show() {
      if (this.data.icons.indexOf("back") !== -1) {
        this.setData({ back: true });
        return;
      }
      let pages = getCurrentPages();
      if (pages.length <= 1) {
        this.setData({ back: false });
        return;
      }
      let path = pages[pages.length - 1].route;
      if (router.tabBars.indexOf(path) === -1) {
        this.setData({ back: true });
      } else {
        this.setData({ back: false });
      }
    },
  },
  methods: {
    initNav() {
      const systemInfo = wx.getSystemInfoSync();
      let navBarHeight = 0;
      if (/ios/i.test(systemInfo.platform)) {
        navBarHeight = 44;
      } else if (systemInfo.platform === "devtools") {
        navBarHeight = 44;
      } else {
        navBarHeight = 48;
      }
      this.setData({
        statusBarHeight: systemInfo.statusBarHeight,
        navBarHeight: navBarHeight,
        height: navBarHeight + systemInfo.statusBarHeight,
      });
    },
    parseIcons(icons) {
      if (!icons) {
        this.setData({ iconList: [] });
      } else {
        let list = [];
        const names = icons.split(",");
        for (let i = 0; i < names.length; i++) {
          let name = names[i].trim();
          if (navItems[name]) {
            list.push(navItems[name]);
          }
        }
        this.setData({ iconList: list, back: icons.indexOf("back") !== -1 });
      }
    },
    handleClick(e) {
      router.push({ path: e.currentTarget.dataset.path });
    },
    handleBack() {
      let pages = getCurrentPages();

      if (pages.length <= 1) {
        router.push({ name: "index" });
      } else {
        wx.navigateBack();
      }
    },
  },
});
